#!/bin/bash
# This script is intended to be used with the install action
# of puppet-cloudpack

set -u
set -e

if [ -f /etc/debian_version ]; then
  apt-get -y install ruby rubygems
else #if [ -f /etc/redhat-release ]; then
  cat >/etc/yum.repos.d/epel.repo <<'EOFYUMREPO'
[prosvc]
name=Puppet Labs PS (Unofficial)
baseurl=http://yum.puppetlabs.com/prosvc/5/$basearch
enabled=1
gpgkey=http://yum.puppetlabs.com/prosvc/RPM-GPG-KEY-prosvc
gpgcheck=1
EOFYUMREPO
  yum install -y ruby rubygems
fi
#gem install guid
#gem install fog
gem install facter <% if options[:facter_version] %>-v<%= options[:facter_version] %><% end %> --no-ri --no-rdoc
gem install puppet <% if options[:puppet_version] %>-v<%= options[:puppet_version] %><% end %> --no-ri --no-rdoc
mkdir -p /etc/puppet /var/lib /var/log /var/run
cat >/etc/puppet/puppet.conf <<'EOFPUPPETCONF'
[main]
  logdir = /var/log/puppet
  rundir = /var/run/puppet
  vardir = /var/lib/puppet
  ssldir = $vardir/ssl
  pluginsync = true
  server = <%= options[:server] %>
  environment = <%= options[:environment] %>
  certname = <%= options[:certname] %>
  report = true
EOFPUPPETCONF

export PATH="$PATH:/var/lib/gems/1.8/bin"
cd <%= options[:tmp_dir] %>

echo "Running: puppet agent --server puppet.bademjoon.com --color=false --waitforcert 5 --test"
puppet agent --server puppet.bademjoon.com --waitforcert 5 --color=false --test | tee puppet_agent.log 
echo "Puppet installation finished!"

